package model;

import controller.RuntimeState;

public abstract class Command extends CodeLine implements Cloneable {

	private static final long serialVersionUID = -5479103040488387406L;
	private int opcode;

	public Command() {
		super();
		this.setName(getCommandName());
	}

	public void execute(RuntimeState runtimeState, Reference... refs) {
		if (refs.length != getParameterCount()) {
			throw new IllegalArgumentException(getName() + " needs "
					+ getParameterCount() + " parameter (got " + refs.length
					+ ")");
		}
		run(runtimeState, refs);
	}

	public abstract void run(RuntimeState runtimeState, Reference... refs);

	public abstract String getCommandName();

	public int getOpcode() {
		return opcode;
	}

	public void setOpcode(int opcode) {
		this.opcode = opcode;
	}

	public Command copy() {
		try {
			Command ncommand = this.getClass().newInstance();
			ncommand.setOpcode(this.getOpcode());
			ncommand.setCommandParameter(this.getCommandParameter());
			ncommand.setLabel(this.getLabel());
			ncommand.setName(this.getName());
			return ncommand;
		} catch (Exception e) {
			return null;
		}
	}
}
